home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / aspisrc.zip / PORT.C < prev    next >
C/C++ Source or Header  |  1992-01-26  |  29KB  |  1,325 lines

  1. /* Supporting routines which may sometimes be missing.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * @(#)port.c 1.15    87/11/05    by John Gilmore, 1986
  22.  *
  23.  * These are routines not available in all environments.
  24.  *
  25.  * I know this introduces an extra level of subroutine calls and is
  26.  * slightly slower.  Frankly, my dear, I don't give a damn.  Let the
  27.  * Missed-Em Vee losers suffer a little.  This software is proud to
  28.  * have been written on a BSD system.
  29.  */
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <signal.h>
  34. #include <errno.h>
  35.  
  36. #if    defined(__MSDOS__) || defined(USG)
  37. #include <fcntl.h>
  38. #else
  39. #include <sys/file.h>
  40. #endif
  41.  
  42. #include "tar.h"
  43. #include "port.h"
  44.  
  45. #ifdef USG
  46. #include <string.h>
  47. #else
  48. extern size_t strlen();
  49. #endif
  50.  
  51. extern long baserec;
  52. /*
  53.  * Some people (e.g. V7) don't have a #define for these.
  54.  */
  55. #ifndef    O_BINARY
  56. #define    O_BINARY    0
  57. #endif
  58. #ifndef    O_RDONLY
  59. #define    O_RDONLY    0
  60. #endif
  61. #ifndef NULL
  62. #define NULL 0
  63. #endif
  64.  
  65. /* JF: modified so all configuration information can appear here, instead of
  66.    being scattered through the file.  Add all the machine-dependent #ifdefs
  67.    here */
  68. #undef WANT_DUMB_GET_DATE/* WANT_DUMB_GET_DATE --> get_date() */
  69. #undef WANT_VALLOC    /* WANT_VALLOC --> valloc() */
  70. #undef WANT_MKDIR    /* WANT_MKDIR --> mkdir() rmdir() */
  71. #undef WANT_STRING    /* WANT_STRING --> index() bcopy() bzero() bcmp() */
  72. #undef WANT_BZERO    /* WANT_BZERO --> bzero() bcmp() execlp() */
  73.             /* EMUL_OPEN3 --> open3() */
  74. #undef WANT_MKNOD    /* WANT_MKNOD --> mknod() link() chown() geteuid() */
  75. #undef WANT_UTILS    /* WANT_UTILS --> panic() ck_*() *_buffer()
  76.                merge_sort() quote_copy_string() un_quote_string() */
  77. #undef WANT_CK_PIPE    /* WANT_CK_PIPE --> ck_pipe() */
  78. #undef WANT_GETWD    /* WANT_GETWD --> getwd() */
  79. #undef WANT_STRSTR    /* WANT_STRSTR --> strstr() */
  80. #undef WANT_FTRUNCATE    /* WANT_FTRUNCATE --> frtruncate() */
  81.  
  82. /* Define only ONE of these four . . . */
  83. /* #undef DOPRNT_MSG    /* Define this one if you have _doprnt() and
  84.                no varargs support */
  85. /* #undef VARARGS_MSG    /* Define this one if you have varargs.h and
  86.                vfprintf() */
  87. /* #undef STDC_MSG     /* Define this one if you are using ANSI C and
  88.                and have vfprintf() */
  89. /* #undef LOSING_MSG    /* Define this one if you don't have any of the
  90.                above */
  91. #ifdef USG
  92. #define WANT_STRING
  93. #define WANT_VALLOC
  94.  
  95. #if defined(sgi) && defined(mips)
  96. #define WANT_GETWD
  97. #endif
  98.  
  99. #if defined(i386)
  100. #define WANT_FTRUNCATE
  101. #endif
  102.  
  103. #endif
  104.  
  105. #ifdef hpux
  106. #define WANT_VALLOC
  107. #endif
  108.  
  109. #ifdef MINIX
  110. #define WANT_BZERO
  111. #endif
  112.  
  113. #ifdef __MSDOS__
  114. char TTY_NAME[] = "con";
  115.  
  116. #define WANT_STRING
  117. #define WANT_MKNOD
  118. #define WANT_UTILS
  119. #define WANT_VALLOC
  120.  
  121. #if (!defined(STDC_MSG) && !defined(DOPRNT_MSG) && !defined(VARARGS_MSG) && !defined(LOSING_MSG))
  122. #ifdef __STDC__
  123. #define STDC_MSG
  124. #else
  125. #define LOSING_MSG
  126. #endif
  127. #endif
  128.  
  129. #else /* not MSDOS */
  130. char TTY_NAME[] ="/dev/tty";
  131.  
  132. #define WANT_UTILS
  133. #define WANT_CK_PIPE
  134. #ifndef HAVE_STRSTR
  135. #define WANT_STRSTR
  136. #endif
  137.  
  138. #if (!defined(STDC_MSG) && !defined(DOPRNT_MSG) && !defined(VARARGS_MSG) && !defined(LOSING_MSG))
  139. #ifdef BSD42
  140. /* BSD systems should do this even if __STDC__, because
  141.    we might be using an ANSI compiler without an ANSI library. (sigh) */
  142. #ifdef sparc
  143. #define LOSING_MSG
  144. #else
  145. #define DOPRNT_MSG
  146. #endif
  147. #else /* not BSD */
  148. #ifdef __STDC__
  149. #define STDC_MSG
  150. #else /* not ANSI C */
  151. #define LOSING_MSG
  152. #endif /* not ANSI C */
  153. #endif /* not BSD */
  154. #endif /* Need to define some form of _MSG */
  155. #endif /* not MSDOS */
  156.  
  157. /* End of system-dependent #ifdefs */
  158.  
  159. #ifdef WANT_DUMB_GET_DATE
  160. /* JF a get_date() routine takes a date/time/etc and turns it into a time_t */
  161. /* This one is a quick hack I wrote in about five minutes to see if the N
  162.    option works.  Someone should replace it with one that works */
  163.  
  164. /* This get_date takes an arg of the form mm/dd/yyyy hh:mm:ss and turns it
  165.    into a time_t .  Its not well tested or anything. . .  */
  166. /* In general, you should use the get_date() supplied in getdate.y */
  167.  
  168. #define OFF_FROM GMT 18000        /* Change for your time zone! */
  169.  
  170. time_t
  171. get_date(str)
  172. char *str;
  173. {
  174.     int month,day,year,hour,minute,second;
  175.     time_t ret;
  176.     int    n;
  177.  
  178. #define SECS_PER_YEAR (365L*SECS_PER_DAY)
  179. #define SECS_PER_LEAP_YEAR (366L*SECS_PER_DAY)
  180.  
  181. #define SECS_PER_DAY (24L*60*60)
  182.     static int days_per_month[2][12] = {
  183.         31,28,31,30,31,30,31,31,30,31,30,31,
  184.         31,29,31,30,31,30,31,31,30,31,30,31
  185.     };
  186.  
  187.     static int days_per_year[2]={365,366};
  188.  
  189.     month=day=year=hour=minute=second=0;
  190.     n=sscanf(str,"%d/%d/%d %d:%d:%d",&month,&day,&year,&hour,&minute,&second);
  191.     if(n<3)
  192.         return 0;
  193.     if(year<100)
  194.         year+=1900;
  195.     if(year<1970)
  196.         return 0;
  197.     ret=0;
  198.  
  199.     ret+=OFF_FROM_GMT;
  200.  
  201.     for(n=1970;n<year;n++)
  202.         if(n%4==0 && n%400!=0)
  203.             ret+=SECS_PER_LEAP_YEAR;
  204.         else
  205.             ret+=SECS_PER_YEAR;
  206.  
  207.     month--;
  208.     for(n=0;n<month;n++) {
  209.         if(year%4==0 && year%400!=0)
  210.             ret+=SECS_PER_DAY*days_per_month[1][n];
  211.         else
  212.             ret+=SECS_PER_DAY*days_per_month[0][n];
  213.     }
  214.     ret+=SECS_PER_DAY*(day-1);
  215.     ret+=second+minute*60+hour*60*60;
  216.     return ret;
  217. }
  218. #endif
  219.  
  220. #ifdef WANT_VALLOC
  221. /*
  222.  * valloc() does a malloc() on a page boundary.  On some systems,
  223.  * this can make large block I/O more efficient.
  224.  */
  225. char *
  226. valloc (size)
  227.     unsigned size;
  228. {
  229.     extern char *malloc ();
  230.     return (malloc (size));
  231. }
  232. #endif
  233. /*
  234.  *                NMKDIR.C
  235.  *
  236.  * Written by Robert Rother, Mariah Corporation, August 1985. 
  237.  *
  238.  * I wrote this out of shear disgust with myself because I couldn't
  239.  * figure out how to do this in /bin/sh.
  240.  *
  241.  * If you want it, it's yours.  All I ask in return is that if you
  242.  * figure out how to do this in a Bourne Shell script you send me
  243.  * a copy.
  244.  *                    sdcsvax!rmr or rmr@uscd
  245. *
  246. * Severely hacked over by John Gilmore to make a 4.2BSD compatible
  247. * subroutine.    11Mar86; hoptoad!gnu
  248. *
  249. * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
  250. * subroutine didn't return EEXIST.  It does now.
  251. */
  252.  
  253. /*
  254.  * Make a directory.  Compatible with the mkdir() system call on 4.2BSD.
  255.  */
  256. #ifdef WANT_MKDIR
  257. int
  258. mkdir(dpath, dmode)
  259.     char *dpath;
  260.     int dmode;
  261. {
  262.     int cpid, status;
  263.     struct stat statbuf;
  264.     extern int errno;
  265.  
  266.     if (stat(dpath,&statbuf) == 0) {
  267.         errno = EEXIST;        /* Stat worked, so it already exists */
  268.         return -1;
  269.     }
  270.  
  271.     /* If stat fails for a reason other than non-existence, return error */
  272.     if (errno != ENOENT) return -1; 
  273.  
  274.     switch (cpid = fork()) {
  275.  
  276.     case -1:            /* Error in fork() */
  277.         return(-1);        /* Errno is set already */
  278.  
  279.     case 0:                /* Child process */
  280.         /*
  281.          * Cheap hack to set mode of new directory.  Since this
  282.          * child process is going away anyway, we zap its umask.
  283.          * FIXME, this won't suffice to set SUID, SGID, etc. on this
  284.          * directory.  Does anybody care?
  285.          */
  286.         status = umask(0);    /* Get current umask */
  287.         status = umask(status | (0777 & ~dmode)); /* Set for mkdir */
  288.         execl("/bin/mkdir", "mkdir", dpath, (char *)0);
  289.         _exit(-1);        /* Can't exec /bin/mkdir */
  290.     
  291.     default:            /* Parent process */
  292.         while (cpid != wait(&status)) ;    /* Wait for kid to finish */
  293.     }
  294.  
  295.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  296.         errno = EIO;        /* We don't know why, but */
  297.         return -1;        /* /bin/mkdir failed */
  298.     }
  299.  
  300.     return 0;
  301. }
  302. int
  303. rmdir(dpath)
  304.     char *dpath;
  305. {
  306.     int cpid, status;
  307.     struct stat statbuf;
  308.     extern int errno;
  309.  
  310.     if (stat(d